/*
    some utils for strings and formatted output
 */

package sUtils;

import java.util.*;

public class Strings {

    public static String getGPS_GMS( double d, char pos, char neg )
    {
        double dd = d<0?-d:d;
        int hh = (int)(dd);
        dd = ((dd-(double)hh)*3600.0);
        int mm = (int)(dd/60.0); dd -= mm*60;
        int ss = (int)(dd); dd -= ss;
        int tt = (int)(dd*100+0.5);
        StringBuffer sb = new StringBuffer(50);
        if (hh<10) sb.append("0"); sb.append(hh); sb.append("° ");
        if (mm<10) sb.append("0"); sb.append(mm); sb.append("' ");
        if (ss<10) sb.append("0"); sb.append(ss); sb.append(".");
        if (tt<10) sb.append("0"); sb.append(tt); sb.append("\" ");
        sb.append( dd<0 ? neg : pos );
        return sb.toString();
    }

    public static String getDateID()
    {
        Calendar dt = Calendar.getInstance();
        int yyyy = dt.get(Calendar.YEAR);
        int mo = dt.get(Calendar.MONTH)+1;
        int dd = dt.get(Calendar.DAY_OF_MONTH);

        StringBuffer sb = new StringBuffer(40);
        sb.append(yyyy);
        sb.append(mo/10); sb.append(mo%10);
        sb.append(dd/10); sb.append(dd%10);
        return sb.toString();
    }

    public static String getTimeID()
    {
        Calendar dt = Calendar.getInstance();
        int hh = dt.get(Calendar.HOUR_OF_DAY);
        int mm = dt.get(Calendar.MINUTE);

        StringBuffer sb = new StringBuffer(40);
        sb.append(hh/10); sb.append(hh%10);
        sb.append(mm/10); sb.append(mm%10);
        return sb.toString();
    }

    public static String toStr( int i, int len )
    {
        StringBuffer sb = new StringBuffer(10);
        int n = 1;
        for ( int j=1; j<len; j++ ) n*=10;
        if (i<0) { sb.append("-"); i=-i; }
        if (i>n) sb.append(i); else
        for ( int j=0; j<len; j++ )
        {
            sb.append((char)('0'+(i/n)%10));
            n/=10;
        }
        return sb.toString();
    }

    public static String toStr( double d, int prev, int post, char leading, String prevS, String postS )
    {
        StringBuffer sb = new StringBuffer(10);
        if (prevS!=null) sb.append(prevS);
        int id = (int)d, n=1;
        boolean minus = false;
        for ( int j=1; j<prev; j++ ) n*=10;
        if (d<0) { if (leading=='0') sb.append("-"); else minus=true; id=-id; d=-d; }

        if (d>n) sb.append(id); else
        for ( int j=0; j<prev; j++ )
        {
            if (id<n && n>1)
            {
                 if (leading!=0) sb.append(leading);
            }
            else
            {
                if (minus) { sb.append("-"); minus=false; }
                sb.append((char)('0'+(id/n)%10));
            }
            n/=10;
        }
        if (post>0)
        {
            double dp, dn=10;
            sb.append(".");
            for ( int j=1; j<=post; j++ )
            {
                dp=d*dn; if (j==post) dp+=0.5;
                sb.append( (char)('0' + (((int)dp)%10)));
                dn*=10;
            }
        }
        if (postS!=null) sb.append(postS);
        return sb.toString();
    }

    public static String toStr( double d, int prev, int post )
    {
        StringBuffer sb = new StringBuffer(10);
        int id = (int)d, n=1;
        boolean minus = false;
        for ( int j=1; j<prev; j++ ) n*=10;
        if (d<0) { minus=true; id=-id; d=-d; }

        if (d>n) sb.append(id); else
        for ( int j=0; j<prev; j++ )
        {
            if (id>=n || n<=1)
            {
                if (minus) { sb.append("-"); minus=false; }
                sb.append((char)('0'+(id/n)%10));
            }
            n/=10;
        }
        if (post>0)
        {
            double dp, dn=10;
            sb.append(".");
            for ( int j=1; j<=post; j++ )
            {
                dp=d*dn; if (j==post) dp+=0.5;
                sb.append( (char)('0' + (((int)dp)%10)));
                dn*=10;
            }
        }
        return sb.toString();
    }

    static public String[] split( String s, char sep )
    {
        int count = 1;
        for (int i=0; i<s.length(); i++) if (s.charAt(i)==sep) count++;

        String arg[] = new String[count];
        int a=0; char z;
        for (int i=0; i<s.length() && a<count; i++)
        {
            z = s.charAt(i);
            if (arg[a]==null) arg[a]="";
            if (z==sep) a++; else if (z!='\n') arg[a] = arg[a] + z;
        }
        return arg;
    }
    
    public static String getTZ( long t )
    {
        Calendar dt = Calendar.getInstance();
        Date dtt = new Date();
        dtt.setTime(t);
        dt.setTime(dtt);
        int yyyy = dt.get(Calendar.YEAR);
        int mo = dt.get(Calendar.MONTH)+1;
        int dd = dt.get(Calendar.DAY_OF_MONTH);
        int hh = dt.get(Calendar.HOUR_OF_DAY);
        int mm = dt.get(Calendar.MINUTE);
        int ss = dt.get(Calendar.SECOND);

        //2010-04-14T11:10:05Z
        StringBuffer sb = new StringBuffer(50);
        sb.append(yyyy); sb.append("-");
        sb.append(mo/10); sb.append(mo%10); sb.append("-");
        sb.append(dd/10); sb.append(dd%10); sb.append("T");
        sb.append(hh/10); sb.append(hh%10); sb.append(":");
        sb.append(mm/10); sb.append(mm%10); sb.append(":");
        sb.append(ss/10); sb.append(ss%10); sb.append("Z");
        return sb.toString();
    }

    public static String getTZ()
    {
        Calendar dt = Calendar.getInstance();
        int yyyy = dt.get(Calendar.YEAR);
        int mo = dt.get(Calendar.MONTH)+1;
        int dd = dt.get(Calendar.DAY_OF_MONTH);
        int hh = dt.get(Calendar.HOUR_OF_DAY);
        int mm = dt.get(Calendar.MINUTE);
        int ss = dt.get(Calendar.SECOND);

        //2010-04-14T11:10:05Z
        StringBuffer sb = new StringBuffer(50);
        sb.append(yyyy); sb.append("-");
        sb.append(mo/10); sb.append(mo%10); sb.append("-");
        sb.append(dd/10); sb.append(dd%10); sb.append("T");
        sb.append(hh/10); sb.append(hh%10); sb.append(":");
        sb.append(mm/10); sb.append(mm%10); sb.append(":");
        sb.append(ss/10); sb.append(ss%10); sb.append("Z");
        return sb.toString();
    }

    public static String getDateTimeStr()
    {
        Calendar dt = Calendar.getInstance();
        int yyyy = dt.get(Calendar.YEAR);
        int mo = dt.get(Calendar.MONTH)+1;
        int dd = dt.get(Calendar.DAY_OF_MONTH);
        int hh = dt.get(Calendar.HOUR_OF_DAY);
        int mm = dt.get(Calendar.MINUTE);
        int ss = dt.get(Calendar.SECOND);

        //2010-04-14T11:10:05Z
        StringBuffer sb = new StringBuffer(50);
        sb.append(dd/10); sb.append(dd%10); sb.append(".");
        sb.append(mo/10); sb.append(mo%10); sb.append(".");
        sb.append(yyyy); sb.append(" ");
        sb.append(hh/10); sb.append(hh%10); sb.append(":");
        sb.append(mm/10); sb.append(mm%10); sb.append(":");
        sb.append(ss/10); sb.append(ss%10);
        return sb.toString();
    }

}
